package com.hehui.partimejob;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowInsets;
import com.google.gson.Gson;
import com.hehui.partimejob.view.NavView;
import com.hehui.partimejob.websocket.WebClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.reflect.ParameterizedType;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * @author 小老板
 * @date 2021/11/23
 */
public abstract class BaseActivity extends SwipeBackActivity {

    public static BaseActivity A;
    public final String TAG = getClass().getSimpleName();
    private NavView navView;

    public NavView getNavView() {
        return navView;
    }

    public void setNavView(NavView navView) {
        this.navView = navView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        A = this;
        //设置activity进入和退出动画
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
        getWindow().getInsetsController().hide(WindowInsets.Type.statusBars());
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
//        getWindow().getDecorView().setBackgroundColor(getColor(R.color.bg));

//        全局更换字体
//        if (typeface == null) {
//            typeface = Typeface.createFromAsset(getAssets(), "Arial.ttf");
//        }
//        LayoutInflaterCompat.setFactory2(LayoutInflater.from(this), new LayoutInflater.Factory2() {
//            @Nullable
//            @Override
//            public View onCreateView(@Nullable View parent, @NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
//                AppCompatDelegate delegate = getDelegate();
//                View view = delegate.createView(parent, name, context, attrs);
//
//                if (view instanceof TextView) {
//                    ((TextView) view).setTypeface(typeface);
//                }
//                if ((view instanceof EditText)) {
//                    ((EditText) view).setTypeface(typeface);
//                }
//                return view;
//            }
//
//            @Nullable
//            @Override
//            public View onCreateView(@NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
//                return null;
//            }
//        });
        setContentView(getLayoutId());

        init();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.slide_back_right_out);
    }

    private BroadcastReceiver receiver;

    public <T> void receiveMsg(AbstractOnMyReceiveListener<T> listener) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WebClient.ACTION_RECEIVE_MESSAGE);
        registerReceiver(receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String stringExtra = intent.getStringExtra(WebClient.KEY_RECEIVED_DATA);
                Log.d(TAG, "onReceive: " + stringExtra);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(stringExtra);
                    String data = jsonObject.optString(WebClient.KEY_RECEIVED_DATA);
                    T t = new Gson().fromJson(data, listener.gettClass());
                    listener.onMessage(t);
                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }
        }, intentFilter);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            unregisterReceiver(receiver);
            receiver = null;
        }
    }

    public abstract static class AbstractOnMyReceiveListener<T> {

        public Class<T> gettClass() {
            return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        }

        public void onMessage(T t) {

        }
    }


    @Override
    public void setContentView(int layoutresid) {
        super.setContentView(layoutresid);
        navView = new NavView();
        navView.createNavForActivity(getWindow(), this);


    }

    /**
     *  获取布局id
     * @return xml布局id
     */
    public abstract int getLayoutId();


    public SharedPreferences getSharedPreferences() {
        return super.getSharedPreferences("PartimeJob", MODE_PRIVATE);
    }


    public void startActivity(Class<?> clazz) {
        super.startActivity(new Intent(this, clazz));
    }

    /**
     * 初始化控件，顶部导航栏
     */
    public abstract void init();
}